package com.jiujiu.analysisFile;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Preconditions;
import com.jiujiu.MustInterface.JiuJiuAnalysis;
import com.jiujiu.model.DataBaseCommon.SqlEntity;
import com.jiujiu.utitls.ContentMap;
import org.apache.log4j.Logger;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
/**
 * @author ZhaoJunZhou
 * @Date 2020/10/12
 */
public class TemplateAnalysis {
    private static final Logger log = Logger.getLogger(TemplateAnalysis.class);
    private File file;
    private Map<String, Object> jsonMap;
    private JSONObject jsonObject;
    private static ExecutorService pool;
    private List<SqlEntity> sqlEntities;
    private JiuJiuAnalysis analysisEntity;
    public TemplateAnalysis(File file,JiuJiuAnalysis _analysisEntity) {
        this.file = file;
        StringBuilder result = new StringBuilder();
        String str = null;
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));

            while ((str = br.readLine()) != null) {
                result.append(str);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Preconditions.checkArgument(!(result.length() < 5),"文件内容为空");
        log.info(String.format("读取到的文件内容为：%s", result.toString()));
        jsonMap = JSONObject.parseObject(result.toString()).toJavaObject(Map.class);
        jsonObject = JSONObject.parseObject(result.toString());
        analysisEntity = _analysisEntity;
    }

    /**
     * 放回解析好之后的json
     * @return
     */
    public void analysis() {
        log.info(String.format("经行执行解析执行,使用的解析类为%s",analysisEntity.getClass()));
        //进行json配置类数据进入
        ContentMap.addParameter(jsonObject.getInnerMap());
        analysisEntity.analysis(jsonObject);
    }

}
